﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Xin478.CSWork.Common;

using NBearLite;

namespace Xin478.CSWork.Client
{
    public class Works
    {

        public static int 任务数量(WhereClip wc)
        {
            return Db.MyWork.Select(MyWork.Works , MyWork.Works.Id.Count()).Where(wc).ToScalar<int>();
        }

        public static IList<WorkInfo> 读取接受任务列表(WhereClip wc,QueryColumn orderby,OrderByClip order, int pageSize,int pageIndex)
        {
            IList<WorkInfo> wis = null;

            IDataReader dr = Db.MyWork.Select(MyWork.Works,
                MyWork.Works.Id,
                MyWork.Works.Title,
                MyWork.Works.PublisherId,
                MyWork.Works.UserId,
                MyWork.Works.Weight,
                MyWork.Works.AddTime,
                MyWork.Works.FinishTime,
                MyWork.Works.LatestTime,
                MyWork.Works.AcceptTime,
                MyWork.Works.PublisherId,
                MyWork.Employees.Name
                )
                .LeftJoin(MyWork.Employees, MyWork.Employees.Id == MyWork.Works.PublisherId)
                .Where(wc)
                .SetSelectRange(pageSize, (pageIndex - 1) * pageSize, orderby)
                .OrderBy(order)
                .ToDataReader();

            if (dr.Read())
            {
                wis = new List<WorkInfo>();
                do
                {
                    WorkInfo wi = new WorkInfo();
                    wi.Id = Convert.ToInt32(dr["Id"]);
                    wi.Title = dr["Title"].ToString();
                    wi.PublisherId = Convert.ToInt32(dr["PublisherId"]);
                    wi.UserId = Convert.ToInt32(dr["UserId"]);
                    wi.Weight = Convert.ToInt32(dr["Weight"]);
                    if (dr["AddTime"] != DBNull.Value) wi.AddTime = Convert.ToDateTime(dr["AddTime"]);
                    if (dr["FinishTime"] != DBNull.Value) wi.FinishTime = Convert.ToDateTime(dr["FinishTime"]);
                    if (dr["LatestTime"] != DBNull.Value) wi.LatestTime = Convert.ToDateTime(dr["LatestTime"]);
                    if (dr["AcceptTime"] != DBNull.Value) wi.AcceptTime = Convert.ToDateTime(dr["AcceptTime"]);
                    wi.PublisherInfo = new EmployeeInfo();
                    wi.PublisherInfo.Name = dr["Name"].ToString();
                    wi.PublisherInfo.Id = wi.PublisherId;
                    wis.Add(wi);

                } while (dr.Read());
            }
            dr.Close();
            return wis;
        }
        public static IList<WorkInfo> 读取发布任务列表(WhereClip wc, QueryColumn orderby, OrderByClip order, int pageSize, int pageIndex)
        {
            IList<WorkInfo> wis = null;

            IDataReader dr = Db.MyWork.Select(MyWork.Works,
                MyWork.Works.Id,
                MyWork.Works.Title,
                MyWork.Works.PublisherId,
                MyWork.Works.UserId,
                MyWork.Works.Weight,
                MyWork.Works.AddTime,
                MyWork.Works.FinishTime,
                MyWork.Works.LatestTime,
                MyWork.Works.AcceptTime,
                MyWork.Works.PublisherId,
                MyWork.Employees.Name
                )
                .LeftJoin(MyWork.Employees, MyWork.Employees.Id == MyWork.Works.UserId)
                .Where(wc)
                .SetSelectRange(pageSize, (pageIndex - 1) * pageSize, orderby)
                .OrderBy(order)
                .ToDataReader();

            if (dr.Read())
            {
                wis = new List<WorkInfo>();
                do
                {
                    WorkInfo wi = new WorkInfo();
                    wi.Id = Convert.ToInt32(dr["Id"]);
                    wi.Title = dr["Title"].ToString();
                    wi.PublisherId = Convert.ToInt32(dr["PublisherId"]);
                    wi.UserId = Convert.ToInt32(dr["UserId"]);
                    wi.Weight = Convert.ToInt32(dr["Weight"]);
                    if (dr["AddTime"] != DBNull.Value) wi.AddTime = Convert.ToDateTime(dr["AddTime"]);
                    if (dr["FinishTime"] != DBNull.Value) wi.FinishTime = Convert.ToDateTime(dr["FinishTime"]);
                    if (dr["LatestTime"] != DBNull.Value) wi.LatestTime = Convert.ToDateTime(dr["LatestTime"]);
                    if (dr["AcceptTime"] != DBNull.Value) wi.AcceptTime = Convert.ToDateTime(dr["AcceptTime"]);
                    wi.UserInfo = new EmployeeInfo();
                    wi.UserInfo.Name = dr["Name"].ToString();
                    wi.UserInfo.Id = wi.UserId;
                    wis.Add(wi);

                } while (dr.Read());
            }
            dr.Close();
            return wis;
        }
        public static WorkInfo 读取任务详细(int workId)
        {
            WorkInfo wi = null;

            IDataReader dr = Db.MyWork.Select(MyWork.Works,
                MyWork.Works.Id,
                MyWork.Works.Title,
                MyWork.Works.PublisherId,
                MyWork.Works.UserId,
                MyWork.Works.Weight,
                MyWork.Works.AddTime,
                MyWork.Works.FinishTime,
                MyWork.Works.LatestTime,
                MyWork.Works.AcceptTime
                )
                .Where(MyWork.Works.Id == workId)
                .ToDataReader();

            if (dr.Read())
            {
                wi = new WorkInfo();
                wi.Id = Convert.ToInt32(dr["Id"]);
                wi.Title = dr["Title"].ToString();
                wi.PublisherId = Convert.ToInt32(dr["PublisherId"]);
                wi.UserId = Convert.ToInt32(dr["UserId"]);
                wi.Weight = Convert.ToInt32(dr["Weight"]);
                if (dr["AddTime"] != DBNull.Value) wi.AddTime = Convert.ToDateTime(dr["AddTime"]);
                if (dr["FinishTime"] != DBNull.Value) wi.FinishTime = Convert.ToDateTime(dr["FinishTime"]);
                if (dr["LatestTime"] != DBNull.Value) wi.LatestTime = Convert.ToDateTime(dr["LatestTime"]);
                if (dr["AcceptTime"] != DBNull.Value) wi.AcceptTime = Convert.ToDateTime(dr["AcceptTime"]);
            }
            dr.Close();

            return wi;
        }
        public static IList<WorkCommandInfo> 读取任务条目(int workId)
        {
            return Db.MyWork.Select(MyWork.WorkCommands).Where(MyWork.WorkCommands.WorkId == workId).ToList<WorkCommandInfo>();
        }
        public static bool 插入(WorkInfo wi)
        {
            if (wi == null ) return false;

            DbTransaction tran = Db.MyWork.BeginTransaction();

            if (Db.MyWork.Insert(MyWork.Works)
                .SetTransaction(tran)
                .AddColumn(MyWork.Works.Id, wi.Id)
                .AddColumn(MyWork.Works.Title, wi.Title)
                .AddColumn(MyWork.Works.AddTime, wi.AddTime)
                .AddColumn(MyWork.Works.AcceptTime, wi.AcceptTime)
                .AddColumn(MyWork.Works.LatestTime, wi.LatestTime)
                .AddColumn(MyWork.Works.FinishTime, wi.FinishTime)
                .AddColumn(MyWork.Works.CustomerId, wi.CustomerId)
                .AddColumn(MyWork.Works.UserId, wi.UserId)
                .AddColumn(MyWork.Works.PublisherId, wi.PublisherId)
                .AddColumn(MyWork.Works.Weight, wi.Weight)
                .AddColumn(MyWork.Works.IsFinished, wi.IsFinished)
                .Execute() != 1)
                return false;


            if (wi.WorkCommandInfos != null && wi.WorkCommandInfos.Count > 0)
            {
                foreach (WorkCommandInfo cmd in wi.WorkCommandInfos)
                {
                    if (Db.MyWork.Insert(MyWork.WorkCommands)
                        .SetTransaction(tran)
                        .AddColumn(MyWork.WorkCommands.Id, cmd.Id)
                        .AddColumn(MyWork.WorkCommands.WorkId, cmd.WorkId)
                        .AddColumn(MyWork.WorkCommands.Request, cmd.Request)
                        .AddColumn(MyWork.WorkCommands.IsFinished, cmd.IsFinished)
                        .AddColumn(MyWork.WorkCommands.LatestTime, cmd.LatestTime)
                        .AddColumn(MyWork.WorkCommands.FinishTime, cmd.FinishTime)
                        .AddColumn(MyWork.WorkCommands.Weight, cmd.Weight)
                        .Execute() == 0)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
            }

            if (wi.WorkFileInfos != null && wi.WorkFileInfos.Count > 0)
            {
                foreach (WorkFileInfo fi in wi.WorkFileInfos)
                {
                    if(Db.MyWork.Insert(MyWork.WorkFiles)
                        .SetTransaction(tran)
                        .AddColumn(MyWork.WorkFiles.Id, fi.Id)
                        .AddColumn(MyWork.WorkFiles.FileName, fi.FileName)
                        .AddColumn(MyWork.WorkFiles.AddTime, fi.AddTime)
                        .AddColumn(MyWork.WorkFiles.WorkId, fi.WorkId)
                        .AddColumn(MyWork.WorkFiles.UserId, fi.UserId)
                        .AddColumn(MyWork.WorkFiles.FileCode, fi.FileCode)
                        .AddColumn(MyWork.WorkFiles.FileDir, fi.FileDir)
                        .Execute() == 0)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
            }

            tran.Commit();
            return true;
        }
        public static bool 批量插入(IList<WorkInfo> works)
        {
            if (works == null || works.Count == 0) return false;

            DbTransaction tran = Db.MyWork.BeginTransaction();
            foreach (WorkInfo wi in works)
            {
                if (Db.MyWork.Insert(MyWork.Works)
                    .SetTransaction(tran)
                    .AddColumn(MyWork.Works.Id, wi.Id)
                    .AddColumn(MyWork.Works.Title, wi.Title)
                    .AddColumn(MyWork.Works.AddTime, wi.AddTime)
                    .AddColumn(MyWork.Works.AcceptTime, wi.AcceptTime)
                    .AddColumn(MyWork.Works.LatestTime, wi.LatestTime)
                    .AddColumn(MyWork.Works.FinishTime, wi.FinishTime)
                    .AddColumn(MyWork.Works.CustomerId, wi.CustomerId)
                    .AddColumn(MyWork.Works.UserId, wi.UserId)
                    .AddColumn(MyWork.Works.PublisherId, wi.PublisherId)
                    .AddColumn(MyWork.Works.Weight, wi.Weight)
                    .AddColumn(MyWork.Works.IsFinished, wi.IsFinished)
                    .Execute() != 1)
                    return false;


                if (wi.WorkCommandInfos != null && wi.WorkCommandInfos.Count > 0)
                {
                    foreach (WorkCommandInfo cmd in wi.WorkCommandInfos)
                    {
                        if (Db.MyWork.Insert(MyWork.WorkCommands)
                            .SetTransaction(tran)
                            .AddColumn(MyWork.WorkCommands.Id, cmd.Id)
                            .AddColumn(MyWork.WorkCommands.WorkId, cmd.WorkId)
                            .AddColumn(MyWork.WorkCommands.Request, cmd.Request)
                            .AddColumn(MyWork.WorkCommands.IsFinished, cmd.IsFinished)
                            .AddColumn(MyWork.WorkCommands.LatestTime, cmd.LatestTime)
                            .AddColumn(MyWork.WorkCommands.FinishTime, cmd.FinishTime)
                            .AddColumn(MyWork.WorkCommands.Weight, cmd.Weight)
                            .Execute() == 0)
                        {
                            tran.Rollback();
                            return false;
                        }
                    }
                }

                if (wi.WorkFileInfos != null && wi.WorkFileInfos.Count > 0)
                {
                    foreach (WorkFileInfo fi in wi.WorkFileInfos)
                    {
                        if (Db.MyWork.Insert(MyWork.WorkFiles)
                            .SetTransaction(tran)
                            .AddColumn(MyWork.WorkFiles.Id, fi.Id)
                            .AddColumn(MyWork.WorkFiles.FileName, fi.FileName)
                            .AddColumn(MyWork.WorkFiles.AddTime, fi.AddTime)
                            .AddColumn(MyWork.WorkFiles.WorkId, fi.WorkId)
                            .AddColumn(MyWork.WorkFiles.UserId, fi.UserId)
                            .AddColumn(MyWork.WorkFiles.FileCode, fi.FileCode)
                            .AddColumn(MyWork.WorkFiles.FileDir, fi.FileDir)
                            .Execute() == 0)
                        {
                            tran.Rollback();
                            return false;
                        }
                    }
                }
            }
            tran.Commit();
            return true;
        }


    }
}
